package com.zhome.gateway.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhome.gateway.constants.Constants;
import com.zhome.gateway.dto.ZhomeRouteDto;
import com.zhome.gateway.repository.entity.RouteInfoEntity;
import com.zhome.gateway.repository.entity.ServerInfoEntity;
import com.zhome.gateway.repository.mapper.RouteInfoMapper;
import com.zhome.gateway.repository.mapper.ServerInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 查询路由信息
 * @author cuiyh9
 * @date 2020/5/9
 */
@Slf4j
@Service
public class RouteService {

    @Autowired
    private RouteInfoMapper routeInfoMapper;

    @Autowired
    private ServerInfoMapper serverInfoMapper;

    public List<RouteInfoEntity> queryAllRouteInfo() {
        QueryWrapper<RouteInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", Constants.NOT_DEL_FLAG);
        List<RouteInfoEntity> list = routeInfoMapper.selectList(queryWrapper);
        return list;
    }

    public List<ServerInfoEntity> queryAllServerInfo() {
        QueryWrapper<ServerInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", Constants.NOT_DEL_FLAG);
        List<ServerInfoEntity> list = serverInfoMapper.selectList(queryWrapper);
        return list;
    }

    public List<ZhomeRouteDto> queryAllRoute() {
        List<RouteInfoEntity>  routeInfoEntityList = queryAllRouteInfo();
        Map<String, ServerInfoEntity> serverInfoEntityMap = queryServerInfoEntityMap();
        List<ZhomeRouteDto> zhomeRouteDtoList = buildZhomeRouteDtoList(routeInfoEntityList, serverInfoEntityMap);
        return zhomeRouteDtoList;
    }

    private Map<String, ServerInfoEntity> queryServerInfoEntityMap() {
        List<ServerInfoEntity> serverInfoEntityList = queryAllServerInfo();
        if (serverInfoEntityList == null) {
            return new HashMap<>(0);
        }

        Map<String, ServerInfoEntity> serverInfoEntityMap = serverInfoEntityList.stream()
                .collect(Collectors.toMap(ServerInfoEntity::getName, serverInfoEntity -> serverInfoEntity));
        return serverInfoEntityMap;
    }

    private List<ZhomeRouteDto> buildZhomeRouteDtoList(List<RouteInfoEntity>  routeInfoEntityList,
                                                       Map<String, ServerInfoEntity> serverInfoEntityMap) {

        List<ZhomeRouteDto> zhomeRouteDtoList = new ArrayList<>();
        if (routeInfoEntityList == null || routeInfoEntityList.isEmpty()) {
            return zhomeRouteDtoList;
        }

        routeInfoEntityList.forEach( routeInfoEntity -> {
            ServerInfoEntity serverInfoEntity = serverInfoEntityMap.get(routeInfoEntity.getServerName());
            if (ObjectUtils.isEmpty(serverInfoEntity)) {
                return ;
            }
            ZhomeRouteDto zhomeRouteDto = new ZhomeRouteDto();
            BeanUtils.copyProperties(routeInfoEntity, zhomeRouteDto);
            zhomeRouteDto.setDomain(serverInfoEntity.getDomain());
            zhomeRouteDto.setType(serverInfoEntity.getType());
            zhomeRouteDtoList.add(zhomeRouteDto);
        });

        return zhomeRouteDtoList;
    }
}
